package com.csh.auth.common.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
/**
 * 分页返回实体
 *
 * @Author: Maverick_Csh
 * @Date: 2024/3/3
 */
@Data
public class PageResult<T> implements Serializable {
    //当前页码值
    private Integer pageNo = 1;

    //每页显示数
    private Integer pageSize = 20;

    //总条数
    private Integer total = 0;

    //总页数
    private Integer totalPages = 0;

    //初始化为一个空的不可变列表。在初始化后，result引用的列表不能被修改，但可以被重新赋值为一个新的列表
    private List<T> result = Collections.emptyList();

    //记录某一页的第一条数据是第几条数据
    private Integer start = 1;

    //记录某一页的最后一条数据是第几条数据
    private Integer end = 0;

    //数据库查到相应的数据后，调用该方法，赋值result、total、totalPages、start、end
    public void setRecords(List<T> result) {
        this.result = result;
        if (result != null && result.size() > 0) {
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPages = (total / this.pageSize) + (total % this.pageSize == 0 ? 0 : 1);
        } else {
            this.totalPages = 0;
        }
        this.start = (this.pageSize > 0 ? (this.pageNo - 1) * this.pageSize : 0) + 1;
        this.end = (this.start - 1 + this.pageSize * (this.pageNo > 0 ? 1 : 0));
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

}

